package cn.elead.chaos.remote.web.system.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.excel.ExcelUtil;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.remote.web.system.service.IRemoteRoleService;
import cn.hutool.core.util.StrUtil;
import cn.elead.chaos.remote.web.system.domain.SysRole;

/**
 * 角色信息Controller
 * 
 * @author luopeng
 * @date 2020-02-26 14:15:00
 */
@RestController
@RequestMapping("/remote/system/role")
public class RemoteRoleController extends BaseController
{
    @Autowired
    private IRemoteRoleService roleService;

    /**
     * 查询角色信息列表
     */
    @GetMapping("/page")
    @RequiresPermissions("system:role:list")
    public TableDataInfo<SysRole> page(SysRole sysRole) {
        return roleService.page(sysRole);
    }
    
    /**
     * 查询角色信息列表
     */
    @GetMapping("/list")
    @RequiresPermissions("system:role:list")
    public List<SysRole> list(SysRole sysRole) {
        return roleService.list(sysRole);
    }

    /**
     * 导出角色信息列表
     */
    @GetMapping("/export")
    @RequiresPermissions("system:role:export")
    @Log(title = "角色信息", businessType = BusinessType.EXPORT)
    public String export(SysRole sysRole) {
        List<SysRole> list = roleService.list(sysRole);
        ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
        return util.exportExcel(list, "role");
    }

    /**
     * 获取角色信息详细信息
     */
    @GetMapping(value = "/{roleId}") 
    @RequiresPermissions("system:role:list")
    public SysRole getInfo(@PathVariable("roleId") Long roleId) {
       	return roleService.getInfo(roleId);
    }

    /**
     * 新增角色信息
     */
    @PostMapping
    @RequiresPermissions("system:role:add")
    @Log(title = "角色信息", businessType = BusinessType.INSERT)
    public int add(@RequestBody SysRole sysRole) {
    	return roleService.add(sysRole);
    }

    /**
     * 修改角色信息
     */
    @PutMapping
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色信息", businessType = BusinessType.UPDATE)
    public int edit(@RequestBody SysRole sysRole) {
        return roleService.edit(sysRole);
    }
    
    
    /**
	 * 状态修改
	 */
	@PutMapping("/changeStatus")
	@RequiresPermissions("system:role:edit")
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	public int changeStatus(@RequestBody SysRole role) {
		return roleService.changeStatus(role);
	}

    /**
     * 删除角色信息
     */
	@DeleteMapping("/{roleId}")
    @RequiresPermissions("system:role:remove")
    @Log(title = "角色信息", businessType = BusinessType.DELETE)
    public int remove(@PathVariable Long[] roleIds){
		String ids = StringUtils.join(roleIds, StrUtil.COMMA);
        return roleService.remove(ids);
    }
	
	/**
	 * 获取角色选择框列表
	 */
	@GetMapping("/optionselect")
	@RequiresPermissions("system:role:list")
	public List<SysRole> optionselect() {
		return roleService.optionselect();
	}
}
